package com.codejiwei.flink.sql;

import com.codejiwei.flink.entity.WaterSensor;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.table.api.Table;
import org.apache.flink.table.api.bridge.java.StreamTableEnvironment;
import org.apache.flink.types.Row;

/**
 * @author jiwei
 * @description
 * @date 2023/5/25 14:04
 */
public class Flink_SQL_BaseUse2 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(5);
        DataStreamSource<WaterSensor> waterSensorStream =
                env.fromElements(new WaterSensor("sensor_1", 1000L, 10),
                        new WaterSensor("sensor_1", 2000L, 20),
                        new WaterSensor("sensor_2", 3000L, 30),
                        new WaterSensor("sensor_1", 4000L, 40),
                        new WaterSensor("sensor_1", 5000L, 50),
                        new WaterSensor("sensor_2", 6000L, 60));

        StreamTableEnvironment tableEnv = StreamTableEnvironment.create(env);
        System.out.println(tableEnv.getConfig().getConfiguration());

        //1. 从流得到一个表
        Table inputTable = tableEnv.fromDataStream(waterSensorStream);
        //2. 把注册为一个临时视图
        tableEnv.createTemporaryView("sensor", inputTable);
        //3. 在临时视图查询数据，并得到一个新表
        Table resultTable = tableEnv.sqlQuery("select * from sensor where id='sensor_1'");
        //4. 显示resultTable的数据
        tableEnv.toDataStream(resultTable, Row.class).print();

        env.execute();
    }
}
